<template>
    <el-form :model="formData" :rules="formRules" ref="dynamicForm" label-width="auto" style="max-width: 600px"
        label-position="top">
        <el-form-item v-for="(field, index) in formFields" :key="index" :label="field.label" :prop="field.prop"
            v-show="!field.show || field.show(formData)">
            <el-input v-if="field.type === 'input'" v-model="formData[field.prop]"
                :placeholder="field.placeholder"></el-input>
            <el-select v-if="field.type === 'select'" :multiple="field.allowMul" :filterable="field.allowCreate"
                :allow-create="field.allowCreate" v-model="formData[field.prop]" :placeholder="field.placeholder">
                <el-option v-for="(option, optionIndex) in field.options" :key="optionIndex" :label="option.label"
                    :value="option.value"></el-option>
            </el-select>
            <!-- 其他类型的表单字段 -->
            <el-input v-if="field.type === 'textarea'" v-model="formData[field.prop]" type="textarea"
            :placeholder="field.placeholder"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm">确定</el-button>
            <el-button @click="cancelSubmit">取消</el-button>
        </el-form-item>
    </el-form>
</template>

<script setup lang="ts">
import scriptServie from '@/api/scriptService'
import { LiteFlowScript } from '@/model/LiteFlow'
import { getScriptType } from '@/utils/type'
import { ElMessage } from 'element-plus'

const props = defineProps(['formFields', 'formData', 'formRules'])
const emit = defineEmits(['submit', 'cancel'])

const submitForm = () => {
    const data = props.formData

    const scriptNode:LiteFlowScript = {
        id:data.id,
        scriptId:data.nodeId,
        scriptName:data.name,
        scriptLanguage:"js",
        scriptData:data.code,
        applicationName:"demo",
        scriptType: getScriptType(data.type)
    }
    scriptServie.update(scriptNode).then(res=>{
        ElMessage.success(res)
    }).catch(err=>ElMessage.error(err))
    emit('submit')
}
const cancelSubmit = () => {
    emit('cancel')
}

</script>